// packageA/pages/detail/detail.js
import Share from "../../../lib/js/share.js";
import { writePhotosAlbum } from "../../../lib/js/util.js";
const font = require('../../../lib/js/font.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.height,
    titleBarHeight: app.globalData.titleBarHeight,
    windowHeight: app.globalData.windowHeight,
    navHeight: app.globalData.navHeight - 2,
    // 组件所需的参数
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '', //导航栏 中间的标题
      color:'#2242A8'
    },
    nowIdx: 0,//当前swiper索引
    indicatorDots: false,
    info: {
      img:['../../../img/dd.png','../../../img/dd.png','../../../img/dd.png']
    },
    nowIndex: "",
    id: "",
    interval: "",
    tdShow: false,
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    font.loadFont(); //下载字体
    if(options.id){
      this.setData({
        id: options.id
      })
    }
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    clearInterval(that.data.interval);
    let time = 0 
    this.secondTime(time)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.interval);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.interval);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      nowIdx: 0,
      nowIndex: "",
    })
    this.getInfo()
    wx.stopPullDownRefresh(); //为停止当前页面下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
 

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    that.forword()
    return {
      title: that.data.info.model,
      path: '/packageA/pages/detail/detail?id=' + that.data.id + '&share=true',
      imageUrl: that.data.info.img[0],
      success: function (res) {
        
        wx.showToast({
          title: "已转发",
          icon: 'none',
          duration: 2000
        })
      }
    }
  },

  //swiper滑动事件
  swiperChange: function (e) {
    this.setData({
      nowIdx: e.detail.current
    })
  },
  //swiper滑动事件
  swiperGo: function (e) {
    this.setData({
      bannerIdx: e.detail.current
    })
  },

  changeOn(e){
      let that = this
      if(Number(e.currentTarget.dataset.id) === 0){
        let str=encodeURIComponent(JSON.stringify(that.data.info.drawings3dUrl));
        wx.navigateTo({
          url: '/packageA/pages/detailCut/detailCut?drawings3dUrl=' + str
        })
        // that.setData({
        //   tdShow:true,
        //   nowIndex: Number(e.currentTarget.dataset.id),
        // })
      }else if(Number(e.currentTarget.dataset.id) === 2){
        that.setData({
          tdShow:true,
          nowIndex: Number(e.currentTarget.dataset.id),
        })
      }else if(Number(e.currentTarget.dataset.id) === 1){
        that.setData({
          nowIndex: Number(e.currentTarget.dataset.id)
        })
        let str=encodeURIComponent(JSON.stringify(that.data.info.cadUrl));
        wx.navigateTo({
          url: '/packageC/pages/drawing/drawing?doxUrl=' + str
        })
      }else{
        that.setData({
          nowIndex: Number(e.currentTarget.dataset.id)
        })
      }
  },
  toReport(e){
    let that = this
     let id= e.currentTarget.dataset.id
     if(id==1){
       let str=encodeURIComponent(JSON.stringify(that.data.info.stressAnalysisUrl2));
        wx.navigateTo({
          url: '/packageC/pages/report/report?doxUrl=' + str + '&title=静应力报告'
        })
     }else{
      let str=encodeURIComponent(JSON.stringify(that.data.info.stressAnalysisUrl1));
      wx.navigateTo({
        url: '/packageC/pages/report/report?doxUrl=' + str + '&title=吊装应力报告'
      })
     }
  },
  //收藏商品
  coll() {
    let that = this;
      app.fly.post('/ma/box/collect', {
        boxId: that.data.info.id,
        status: 1,
      })
        .then(function (res) {
          if (res.code === 0) {
            that.data.info.collected = true;
            that.setData({
              info: that.data.info
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 2000
            });
          }
        })
  },
  //取消收藏
  delColl() {
    let that = this;
      app.fly.post('/ma/box/collect', {
        boxId: that.data.info.id,
        status: 0,
      })
        .then(function (res) {
          if (res.code === 0) {
            that.data.info.collected = false;
            that.setData({
              info: that.data.info
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 2000
            });
          }
        })
  },
 
 //收藏商品
 zan() {
  let that = this;
    app.fly.post('/ma/box/like', {
      boxId: that.data.info.id,
      status: 1,
    })
      .then(function (res) {
        if (res.code === 0) {
          that.data.info.liked = true;
          that.setData({
            info: that.data.info
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 2000
          });
        }
      })
},
//取消收藏
delZan() {
  let that = this;
    app.fly.post('/ma/box/like', {
      boxId: that.data.info.id,
      status: 0,
    })
      .then(function (res) {
        if (res.code === 0) {
          that.data.info.liked = false;
          that.setData({
            info: that.data.info
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 2000
          });
        }
      })
},

forword(){
  let that = this
  app.fly.post('/ma/box/forward',{
    boxId: that.data.id
  }
    )
      .then(function (res) {

        if (res.code === 0) {
           
        } else {
          
        }
      })
},
  //去登陆
  goAuth() {
    wx.navigateTo({
      url: '/packageD/pages/authorize/authorize'
    })
  },
  toEvent(){
    let that = this
    clearInterval(that.data.interval);
    wx.navigateTo({
      url: '/packageA/pages/detailEvent/detailEvent?id=' + this.data.id
    })
  },
  getInfo() {
    let that = this;
    app.fly.get('/ma/box/detail/' + that.data.id
    )
      .then(function (res) {

        if (res.code === 0) {
          //  res.data.img =  res.data.imageUrl.split(',');
           that.setData({
              info: res.data
           })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 2000
          });
        }
      })
  },
  

  secondTime(time) {
    let that = this;
  
    that.setData({
      interval: setInterval(function () {
        time++
        if(time%10===0){
          that.setStatic()
        }
      }, 1000)
    })
  },
  setStatic(){
    let that = this
     app.fly.post('/ma/box/view',{
       boxId: that.data.id
     }).then(function (res) {

        
        
      })
  },
  previwDis(e) {
    var urls = [];
    var src = e.currentTarget.dataset.src;//获取data-src
    var index = e.currentTarget.dataset.index;
    console.log(this.data.info.comment_list[index])
    for (var i = 0; i < this.data.info.comment_list[index].images.length; i++) {
    
      urls[i] = this.data.info.comment_list[index].images[i];
    }
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: urls
    })
  },
  goList() {
    wx.navigateTo({
      url: '/packageB/pages/discussList/discussList?id=' + this.data.id + '&goods_id=' + this.data.info.id + '&store_id=' + this.data.store_id + '&activity_type=' + this.data.activity_type
    })
  },
 
  cancelShow(){
      this.setData({
        tdShow: false,
      })
  },


  previewImage: function (e) {
    let that = this;
    // let file = e.currentTarget.dataset.src;
    wx.previewImage({
      current: that.data.info.banners[that.data.nowIdx], // 当前显示图片的http链接   
      urls: that.data.info.banners // 需要预览的图片http链接列表   
    })
  },
  
  // 保存图片到系统相册
  saveImageToPhotosAlbum(saveUrl) {
    return new Promise((resolve, reject) => {
      wx.saveImageToPhotosAlbum({
        filePath: saveUrl,
        success: (res) => {
          wx.showToast({
            title: '保存成功',
            duration: 1000,
          })
          resolve()
        }
      })
    })
  },

  copyText: function (e) {
    app.copyText(e.currentTarget.dataset.text)
  },


  //判断规格是否选择完整
  isCheckedAllSpec: function () {
    return !this.getCheckedSpecValue().some(function (v) {
      if (v.valueId == 0) {
        return true;
      }
    });
  },
})